package green.util

public class UniqueTimestampId implements Serializable {

  private static final String SERIAL_SEPARATOR = "_"

  private Date date
  private long id

  public UniqueTimestampId(String serialized = null) {
    if (serialized == null) {
      date = new Date()
      id = new Random().next(100000)
    } else {
      def parts = serialized.split(SERIAL_SEPARATOR)
      date = new Date(Long.parseLong(parts[0]))
      id = Long.parseLong(parts[1])
    }
  }

  public String toString() {
    return date.time.toString() + SERIAL_SEPARATOR + id.toString()
  }

  public boolean equals(Object o) {
    return o.toString().equals(toString())
  }

  public int hashCode() {
    return toString().hashCode()
  }

}